<html lang="en-US">

<head>
    
<title>Nginx安装 - 花落雨忧</title>

<meta property="og:title" content="Nginx安装 - 花落雨忧">



    



    
    <meta property="description" content="在./docker-compose中，增加service，实例如下
[&amp;hellip;] version: &amp;#34;3.7&amp;#34; services: # nginx web服务器 nginx: image: nginx:1.21 container_name: &amp;#34;nginx&amp;#34; # 运行依赖，保证其它程序先启动 # 如果还有其它程序，则依次添加在这里 depends_on: - &amp;hellip;">
    <meta property="og:description" content="在./docker-compose中，增加service，实例如下
[&amp;hellip;] version: &amp;#34;3.7&amp;#34; services: # nginx web服务器 nginx: image: nginx:1.21 container_name: &amp;#34;nginx&amp;#34; # 运行依赖，保证其它程序先启动 # 如果还有其它程序，则依次添加在这里 depends_on: - &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.asia/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.asia/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.asia/js/jquery.min.js"></script>




<link href="https://www.lican.asia/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>

        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/tech/nginx%E5%AE%89%E8%A3%85/">Nginx安装</a>
                                        </h2>
                                        
                                    </div>
                                    <div class="post-content">
                                        <h3 id="nginx安装">nginx安装</h3>
<blockquote>
<p>在./docker-compose中，增加service，实例如下</p>
</blockquote>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">version</span>: <span style="color:#e6db74">&#34;3.7&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">services</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#75715e"># nginx web服务器</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">nginx</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">nginx:1.21</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">container_name</span>: <span style="color:#e6db74">&#34;nginx&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># 运行依赖，保证其它程序先启动</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># 如果还有其它程序，则依次添加在这里</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">depends_on</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">php</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># 指定时区</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">environment</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">TZ=Asia/Shanghai</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># 数据卷挂载</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">volumes</span>:
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># 配置挂载到容器，在第一次启动容器后，复制出来，然后重建容器</span>
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># - ./nginx/nginx.conf:/etc/nginx/nginx.conf</span>
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># - ./nginx/conf.d:/etc/nginx/conf.d</span>
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># 证书目录</span>
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># ./nginx/ssl:/ssl</span>
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># 数据卷挂载到容器</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">./front:/front</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">./back:/back</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># 日志</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">logging</span>:
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">driver</span>: <span style="color:#ae81ff">json-file</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">options</span>:
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">max-size</span>: <span style="color:#e6db74">&#34;10M&#34;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">max-file</span>: <span style="color:#e6db74">&#34;10&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># 容器发生意外时，能自动重启</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">restart</span>: <span style="color:#ae81ff">always</span>
</span></span></code></pre></div><h3 id="nginx-配置参考">nginx 配置参考</h3>
<h2 id="1nginxconf">1、nginx.conf</h2>
<blockquote>
<p>client_max_body_size 需要配置一下，否则上传大文件会失败</p>
</blockquote>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">user</span>  <span style="color:#a6e22e">nginx</span>;
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">worker_processes</span>  <span style="color:#a6e22e">auto</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">error_log</span>  <span style="color:#f92672">/</span><span style="color:#66d9ef">var</span><span style="color:#f92672">/</span><span style="color:#a6e22e">log</span><span style="color:#f92672">/</span><span style="color:#a6e22e">nginx</span><span style="color:#f92672">/</span><span style="color:#66d9ef">error</span>.<span style="color:#a6e22e">log</span> <span style="color:#a6e22e">notice</span>;
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">pid</span>        <span style="color:#f92672">/</span><span style="color:#66d9ef">var</span><span style="color:#f92672">/</span><span style="color:#a6e22e">run</span><span style="color:#f92672">/</span><span style="color:#a6e22e">nginx</span>.<span style="color:#a6e22e">pid</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">events</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">worker_connections</span>  <span style="color:#ae81ff">1024</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">http</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">include</span>       <span style="color:#f92672">/</span><span style="color:#a6e22e">etc</span><span style="color:#f92672">/</span><span style="color:#a6e22e">nginx</span><span style="color:#f92672">/</span><span style="color:#a6e22e">mime</span>.<span style="color:#a6e22e">types</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">default_type</span>  <span style="color:#a6e22e">application</span><span style="color:#f92672">/</span><span style="color:#a6e22e">octet</span><span style="color:#f92672">-</span><span style="color:#a6e22e">stream</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">log_format</span>  <span style="color:#a6e22e">main</span>  <span style="color:#960050;background-color:#1e0010">&#39;$</span><span style="color:#a6e22e">remote_addr</span> <span style="color:#f92672">-</span> <span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">remote_user</span> [<span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">time_local</span>] <span style="color:#e6db74">&#34;$request&#34;</span> <span style="color:#960050;background-color:#1e0010">&#39;</span>
</span></span><span style="display:flex;"><span>                      <span style="color:#960050;background-color:#1e0010">&#39;$</span><span style="color:#a6e22e">status</span> <span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">body_bytes_sent</span> <span style="color:#e6db74">&#34;$http_referer&#34;</span> <span style="color:#960050;background-color:#1e0010">&#39;</span>
</span></span><span style="display:flex;"><span>                      <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#e6db74">&#34;$http_user_agent&#34;</span> <span style="color:#e6db74">&#34;$http_x_forwarded_for&#34;</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">access_log</span>  <span style="color:#f92672">/</span><span style="color:#66d9ef">var</span><span style="color:#f92672">/</span><span style="color:#a6e22e">log</span><span style="color:#f92672">/</span><span style="color:#a6e22e">nginx</span><span style="color:#f92672">/</span><span style="color:#a6e22e">access</span>.<span style="color:#a6e22e">log</span>  <span style="color:#a6e22e">main</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">sendfile</span>        <span style="color:#a6e22e">on</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#a6e22e">tcp_nopush</span>     <span style="color:#a6e22e">on</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">keepalive_timeout</span>  <span style="color:#ae81ff">65</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">开启gzip</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">gzip</span> <span style="color:#a6e22e">on</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">启用gzip压缩的最小文件</span><span style="color:#960050;background-color:#1e0010">；</span><span style="color:#a6e22e">小于设置值的文件将不会被压缩</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">gzip_min_length</span> <span style="color:#ae81ff">1</span><span style="color:#a6e22e">k</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">gzip</span> <span style="color:#a6e22e">压缩级别</span> <span style="color:#ae81ff">1</span><span style="color:#f92672">-</span><span style="color:#ae81ff">9</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">gzip_comp_level</span> <span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">进行压缩的文件类型</span><span style="color:#960050;background-color:#1e0010">。</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">gzip_types</span> <span style="color:#a6e22e">text</span><span style="color:#f92672">/</span><span style="color:#a6e22e">plain</span> <span style="color:#a6e22e">application</span><span style="color:#f92672">/</span><span style="color:#a6e22e">javascript</span> <span style="color:#a6e22e">application</span><span style="color:#f92672">/</span><span style="color:#a6e22e">x</span><span style="color:#f92672">-</span><span style="color:#a6e22e">javascript</span> <span style="color:#a6e22e">text</span><span style="color:#f92672">/</span><span style="color:#a6e22e">css</span> <span style="color:#a6e22e">application</span><span style="color:#f92672">/</span><span style="color:#a6e22e">xml</span> <span style="color:#a6e22e">text</span><span style="color:#f92672">/</span><span style="color:#a6e22e">javascript</span> <span style="color:#a6e22e">application</span><span style="color:#f92672">/</span><span style="color:#a6e22e">x</span><span style="color:#f92672">-</span><span style="color:#a6e22e">httpd</span><span style="color:#f92672">-</span><span style="color:#a6e22e">php</span> <span style="color:#a6e22e">image</span><span style="color:#f92672">/</span><span style="color:#a6e22e">jpeg</span> <span style="color:#a6e22e">image</span><span style="color:#f92672">/</span><span style="color:#a6e22e">gif</span> <span style="color:#a6e22e">image</span><span style="color:#f92672">/</span><span style="color:#a6e22e">png</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">是否在http</span> <span style="color:#a6e22e">header中添加Vary</span>: <span style="color:#a6e22e">Accept</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Encoding</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">建议开启</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">gzip_vary</span> <span style="color:#a6e22e">on</span>;
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">这个最好配置一下</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">否则上传大文件会失败</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">client_max_body_size</span> <span style="color:#ae81ff">100</span><span style="color:#a6e22e">m</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">include</span> <span style="color:#f92672">/</span><span style="color:#a6e22e">etc</span><span style="color:#f92672">/</span><span style="color:#a6e22e">nginx</span><span style="color:#f92672">/</span><span style="color:#a6e22e">conf</span>.<span style="color:#a6e22e">d</span><span style="color:#f92672">/*</span>.<span style="color:#a6e22e">conf</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="vhost配置">vhost配置</h3>
<h4 id="农场示例">农场，示例</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">server</span> {
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">listen</span> <span style="color:#ae81ff">80</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">host主机</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">可以多个</span>
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">server_name</span> <span style="color:#a6e22e">host1</span> <span style="color:#a6e22e">host2</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">server_name</span> <span style="color:#a6e22e">farm</span>.<span style="color:#a6e22e">xxx</span>.<span style="color:#a6e22e">com</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">root</span> <span style="color:#f92672">/</span><span style="color:#a6e22e">front</span><span style="color:#f92672">/</span><span style="color:#a6e22e">farm</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">index</span> <span style="color:#a6e22e">index</span>.<span style="color:#a6e22e">html</span> <span style="color:#a6e22e">index</span>.<span style="color:#a6e22e">htm</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h4 id="农场大数据示例">农场大数据，示例</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">server</span> {
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">listen</span> <span style="color:#ae81ff">80</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">server_name</span> <span style="color:#a6e22e">farm</span><span style="color:#f92672">-</span><span style="color:#a6e22e">admin</span>.<span style="color:#a6e22e">xxx</span>.<span style="color:#a6e22e">com</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">root</span> <span style="color:#f92672">/</span><span style="color:#a6e22e">front</span><span style="color:#f92672">/</span><span style="color:#a6e22e">farm</span><span style="color:#f92672">-</span><span style="color:#a6e22e">admin</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">index</span> <span style="color:#a6e22e">index</span>.<span style="color:#a6e22e">html</span> <span style="color:#a6e22e">index</span>.<span style="color:#a6e22e">htm</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h4 id="农场销售页面示例">农场销售页面，示例</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">server</span> {
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">listen</span> <span style="color:#ae81ff">80</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">host主机</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">可以多个</span>
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">server_name</span> <span style="color:#a6e22e">host1</span> <span style="color:#a6e22e">host2</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">server_name</span> <span style="color:#a6e22e">farm</span><span style="color:#f92672">-</span><span style="color:#a6e22e">sales</span>.<span style="color:#a6e22e">xxx</span>.<span style="color:#a6e22e">com</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">root</span> <span style="color:#f92672">/</span><span style="color:#a6e22e">front</span><span style="color:#f92672">/</span><span style="color:#a6e22e">farm</span><span style="color:#f92672">-</span><span style="color:#a6e22e">sales</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">index</span> <span style="color:#a6e22e">index</span>.<span style="color:#a6e22e">html</span> <span style="color:#a6e22e">index</span>.<span style="color:#a6e22e">htm</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h4 id="农场溯源示例">农场溯源，示例</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">server</span> {
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">listen</span> <span style="color:#ae81ff">80</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">host主机</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">可以多个</span>
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">server_name</span> <span style="color:#a6e22e">host1</span> <span style="color:#a6e22e">host2</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">server_name</span> <span style="color:#a6e22e">farm</span><span style="color:#f92672">-</span><span style="color:#a6e22e">source</span>.<span style="color:#a6e22e">xxx</span>.<span style="color:#a6e22e">com</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">root</span> <span style="color:#f92672">/</span><span style="color:#a6e22e">front</span><span style="color:#f92672">/</span><span style="color:#a6e22e">farm</span><span style="color:#f92672">-</span><span style="color:#a6e22e">source</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">index</span> <span style="color:#a6e22e">index</span>.<span style="color:#a6e22e">html</span> <span style="color:#a6e22e">index</span>.<span style="color:#a6e22e">htm</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h4 id="php">php</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">server</span> {
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">listen</span> <span style="color:#ae81ff">80</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">server_name</span> <span style="color:#a6e22e">farm</span><span style="color:#f92672">-</span><span style="color:#a6e22e">api</span>.<span style="color:#a6e22e">xxx</span>.<span style="color:#a6e22e">com</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">root</span> <span style="color:#f92672">/</span><span style="color:#a6e22e">back</span><span style="color:#f92672">/</span><span style="color:#a6e22e">public</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">index</span> <span style="color:#a6e22e">index</span>.<span style="color:#a6e22e">php</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">location</span> <span style="color:#f92672">/</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">下面三行是为了支持跨域</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">add_header</span> <span style="color:#a6e22e">Access</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Control</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Allow</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Origin</span> <span style="color:#f92672">*</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">add_header</span> <span style="color:#a6e22e">Access</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Control</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Allow</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Headers</span> <span style="color:#a6e22e">X</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Requested</span><span style="color:#f92672">-</span><span style="color:#a6e22e">With</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">add_header</span> <span style="color:#a6e22e">Access</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Control</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Allow</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Methods</span> <span style="color:#a6e22e">GET</span>,<span style="color:#a6e22e">POST</span>,<span style="color:#a6e22e">OPTIONS</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> (!<span style="color:#f92672">-</span><span style="color:#a6e22e">e</span> <span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">request_filename</span>){
</span></span><span style="display:flex;"><span>       <span style="color:#a6e22e">rewrite</span>  ^(.<span style="color:#f92672">*</span>)<span style="color:#960050;background-color:#1e0010">$</span>  <span style="color:#f92672">/</span><span style="color:#a6e22e">index</span>.<span style="color:#a6e22e">php</span><span style="color:#f92672">/</span><span style="color:#960050;background-color:#1e0010">$</span><span style="color:#ae81ff">1</span>  <span style="color:#a6e22e">last</span>;
</span></span><span style="display:flex;"><span>       <span style="color:#66d9ef">break</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">location</span> ~ <span style="color:#960050;background-color:#1e0010">\</span>.<span style="color:#a6e22e">php</span>(.<span style="color:#f92672">*</span>)<span style="color:#960050;background-color:#1e0010">$</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">fastcgi_pass</span>   <span style="color:#a6e22e">php</span>:<span style="color:#ae81ff">9000</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">fastcgi_index</span>  <span style="color:#a6e22e">index</span>.<span style="color:#a6e22e">php</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">fastcgi_split_path_info</span>  ^((<span style="color:#960050;background-color:#1e0010">?</span><span style="color:#a6e22e">U</span>).<span style="color:#f92672">+</span><span style="color:#960050;background-color:#1e0010">\</span>.<span style="color:#a6e22e">php</span>)(<span style="color:#f92672">/</span><span style="color:#960050;background-color:#1e0010">?</span>.<span style="color:#f92672">+</span>)<span style="color:#960050;background-color:#1e0010">$</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">fastcgi_param</span>  <span style="color:#a6e22e">SCRIPT_FILENAME</span>   <span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">document_root</span><span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">fastcgi_script_name</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">fastcgi_param</span>  <span style="color:#a6e22e">PATH_INFO</span>  <span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">fastcgi_path_info</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">include</span>        <span style="color:#a6e22e">fastcgi_params</span>;
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h4 id="hls直播">hls直播</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">server</span> {
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">listen</span> <span style="color:#ae81ff">80</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">server_name</span> <span style="color:#a6e22e">live</span>.<span style="color:#a6e22e">xxx</span>.<span style="color:#a6e22e">com</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">root</span> <span style="color:#f92672">/</span><span style="color:#a6e22e">srs</span><span style="color:#f92672">/</span><span style="color:#a6e22e">hls</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">location</span> <span style="color:#f92672">/</span><span style="color:#a6e22e">live</span> {  <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">新摄像头服务</span>(<span style="color:#a6e22e">dev</span>) <span style="color:#a6e22e">HLS</span>
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">types</span> {
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">application</span><span style="color:#f92672">/</span><span style="color:#a6e22e">vnd</span>.<span style="color:#a6e22e">apple</span>.<span style="color:#a6e22e">mpegurl</span> <span style="color:#a6e22e">m3u8</span>;
</span></span><span style="display:flex;"><span>          <span style="color:#a6e22e">video</span><span style="color:#f92672">/</span><span style="color:#a6e22e">mp2t</span> <span style="color:#a6e22e">ts</span>;
</span></span><span style="display:flex;"><span>      }
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">add_header</span> <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">Access</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Control</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Allow</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Origin</span><span style="color:#e6db74">&#39; &#39;</span><span style="color:#f92672">*</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">add_header</span> <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">Access</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Control</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Allow</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Credentials</span><span style="color:#e6db74">&#39; &#39;</span><span style="color:#66d9ef">true</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">add_header</span> <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">Access</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Control</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Allow</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Methods</span><span style="color:#e6db74">&#39; &#39;</span><span style="color:#a6e22e">GET</span>, <span style="color:#a6e22e">POST</span>, <span style="color:#a6e22e">OPTIONS</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">add_header</span> <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">Access</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Control</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Allow</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Headers</span><span style="color:#e6db74">&#39; &#39;</span><span style="color:#f92672">*</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">add_header</span> <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">Cache</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Control</span><span style="color:#e6db74">&#39; &#39;</span><span style="color:#a6e22e">no</span><span style="color:#f92672">-</span><span style="color:#a6e22e">cache</span><span style="color:#960050;background-color:#1e0010">&#39;</span>;
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h4 id="https配置支持">https配置支持</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">server</span> {
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">listen</span> <span style="color:#ae81ff">443</span> <span style="color:#a6e22e">ssl</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">注意这里的路径ssl为容器nginx的路径</span>
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">使用前</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">注意在docker</span><span style="color:#f92672">-</span><span style="color:#a6e22e">compose中做数据卷映射</span>, <span style="color:#a6e22e">如</span><span style="color:#960050;background-color:#1e0010">：</span> .<span style="color:#f92672">/</span><span style="color:#a6e22e">nginx</span><span style="color:#f92672">/</span><span style="color:#a6e22e">ssl</span>:<span style="color:#f92672">/</span><span style="color:#a6e22e">ssl</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">ssl_certificate</span> <span style="color:#f92672">/</span><span style="color:#a6e22e">ssl</span><span style="color:#f92672">/</span><span style="color:#a6e22e">fullchain</span>.<span style="color:#a6e22e">pem</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">ssl_certificate_key</span> <span style="color:#f92672">/</span><span style="color:#a6e22e">ssl</span><span style="color:#f92672">/</span><span style="color:#a6e22e">key</span>.<span style="color:#a6e22e">pem</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">OCSP</span> <span style="color:#a6e22e">Stapling</span> <span style="color:#a6e22e">开启</span><span style="color:#960050;background-color:#1e0010">。</span><span style="color:#a6e22e">OCSP是用于在线查询证书吊销情况的服务</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">使用OCSP</span> <span style="color:#a6e22e">Stapling能将证书有效状态的信息缓存到服</span> <span style="color:#a6e22e">务器</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">提高</span> <span style="color:#a6e22e">TLS</span> <span style="color:#a6e22e">握手速度</span>
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#a6e22e">ssl_stapling</span> <span style="color:#a6e22e">on</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#a6e22e">ssl_stapling_verify</span> <span style="color:#a6e22e">on</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#a6e22e">ssl_trusted_certificate</span>  <span style="color:#f92672">/</span><span style="color:#a6e22e">etc</span><span style="color:#f92672">/</span><span style="color:#a6e22e">nginx</span><span style="color:#f92672">/</span><span style="color:#a6e22e">ssl</span><span style="color:#f92672">/*</span>.<span style="color:#a6e22e">dev</span>.<span style="color:#a6e22e">nongbotech</span>.<span style="color:#a6e22e">cn</span><span style="color:#f92672">/</span><span style="color:#a6e22e">fullchain</span>.<span style="color:#a6e22e">pem</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#a6e22e">resolver</span> <span style="color:#ae81ff">8.8.8.8</span> <span style="color:#ae81ff">8.8.4.4</span> <span style="color:#ae81ff">1.1.1.1</span> <span style="color:#a6e22e">valid</span>=<span style="color:#ae81ff">60</span><span style="color:#a6e22e">s</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#a6e22e">resolver_timeout</span> <span style="color:#ae81ff">20</span><span style="color:#a6e22e">s</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">#</span> <span style="color:#a6e22e">http请求重定向到https</span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">location</span> <span style="color:#f92672">/</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> (<span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">ssl_protocol</span> = <span style="color:#e6db74">&#34;&#34;</span>) {<span style="color:#66d9ef">return</span> <span style="color:#ae81ff">301</span> <span style="color:#a6e22e">https</span>:<span style="color:#75715e">//$host$request_uri;}
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h4 id="反向代理设置代理头">反向代理，设置代理头</h4>
<p>使后端可以获取真实的host地址及ip</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span>    <span style="color:#a6e22e">proxy_set_header</span>   <span style="color:#a6e22e">Host</span>             <span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">host</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">proxy_set_header</span>   <span style="color:#a6e22e">X</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Real</span><span style="color:#f92672">-</span><span style="color:#a6e22e">IP</span>        <span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">remote_addr</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">proxy_set_header</span>   <span style="color:#a6e22e">X</span><span style="color:#f92672">-</span><span style="color:#a6e22e">Forwarded</span><span style="color:#f92672">-</span><span style="color:#a6e22e">For</span>  <span style="color:#960050;background-color:#1e0010">$</span><span style="color:#a6e22e">proxy_add_x_forwarded_for</span>;
</span></span></code></pre></div><h1 id="证书生成">证书生成</h1>
<p>这里采用免费证书生成工具acme</p>
<h5 id="一安装acme">一、安装acme</h5>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>curl https://get.acme.sh | sh -s email<span style="color:#f92672">=</span>my@example.com
</span></span></code></pre></div><h5 id="二设置别名">二、设置别名</h5>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>acme.sh<span style="color:#f92672">=</span>~/.acme.sh/acme.sh.
</span></span></code></pre></div><h5 id="三设置自动升级">三、设置自动升级</h5>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>acme.sh --upgrade --auto-upgrade
</span></span></code></pre></div><h5 id="四证书签发">四、证书签发</h5>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># 证书安装</span>
</span></span><span style="display:flex;"><span>acme.sh --issue -d example.com -w /home/wwwroot/example.com
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 安装证书到nginx</span>
</span></span><span style="display:flex;"><span>acme.sh --install-cert -d example.com <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>--key-file       /data/www/nginx/ssl/example.com/key.pem  <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>--fullchain-file /data/www/nginx/ssl/example.com/cert.pem <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>--reloadcmd     <span style="color:#e6db74">&#34;docker restart nginx&#34;</span>
</span></span></code></pre></div><blockquote>
<p>首次安装证书时，需要先屏蔽ssl及证书路径配置，等生成成功后在加上这些配置</p>
</blockquote>


                                        
                                    </div>

                                    

                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.asia/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    <section id="qrcode" class="widget widget_media_image">
        <h2 class="widget-title">微信公众号</h2>
        <img width="258" height="258"
            src="https://www.lican.asia/wechat/lican.png"
            class="image wp-image-5514  attachment-full size-full" alt=""
            style="max-width: 100%; height: auto;"
            sizes="(max-width: 258px) 100vw, 258px">
    </section>
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.asia All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>